from pywinauto import application
from pywinauto import Desktop
from pynput.keyboard import Key, Controller  # 导入按键控制
import time
import lackey

# 控制键盘
keyboard = Controller()
# 启动app，可以把uia和win32理解成控件分析模式
app = application.Application(backend="uia")
# 当应用已经打开了我们就用connect，没打开用start
try:
    app.connect(path=r"E:\虚拟机vmware\vmware.exe")
    # 链接的方式点击桌面任务栏的正在运行程序print_control_identifiers()
    dlg = Desktop(backend="uia").任务栏.运行中的程序.child_window(title="VMware Workstation Pro - 1 个运行窗口",
                                                         auto_id="VMware.Workstation.vmui",
                                                         control_type="Button").click()
except:
    app.start(r"E:\虚拟机vmware\vmware.exe")
# 调整窗口大小,上一次关闭窗口已经是最大化了，没必要了
# if app.window(best_match="VMware Workstation").还原:
#     app.window(best_match="VMware Workstation").还原.click()
# if app.window(best_match="VMware Workstation").最大化:
#     app.window(best_match="VMware Workstation").最大化.click()
# 选择虚拟机
app.window(best_match="VMware Workstation").FreeBSD.select()
# 回车确认选择的虚拟机
keyboard.press(Key.enter)
keyboard.release(Key.enter)
# lackey.wait("继续运行.png", 5)
pattern = lackey.Pattern("1.png")
# define main screen
screen = lackey.Screen()

# find element on a screen
screen.find(pattern) # error